<?php defined('SYSPATH') or die('No direct script access.');

	class Model_Services_Samplingperiod extends Model implements Jqgrid_GridListener{
	
		public $samplingperiod;
		
		public function count($criteria = false){
			return ORM::factory("dao_samplingperiod")->count_all();
		}
			
		public function getList($criteria = false){
			
			if (!$criteria)	$samplingperiod_list = ORM::factory("dao_samplingperiod")->find_all()->as_array();
			else{
				if ($criteria->isSearch){
					$samplingperiod_list = ORM::factory("dao_samplingperiod")
									->where($criteria->getSQLField("sampling_periods"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->offset($criteria->calcOffset())
									->find_all()
									->as_array();
							
				}else{
					$samplingperiod_list = ORM::factory("dao_samplingperiod")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
				}
							
			} 
		
			$dataset = array();
			foreach ($samplingperiod_list as $samplingperiod){
				$dataset[$samplingperiod->id] = array($samplingperiod->pc_description,$samplingperiod->k_value);
			}
			
			return $dataset;
		}
	
		/**
		*
		* get all sampling period from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of samplingperiod
		*/
		public function getListFromMinID($minId, $maxAmount){
			return ORM::factory("dao_samplingperiod")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
		}
		
		public function get($id){
			return ORM::factory("dao_samplingperiod")->find($id);
		}
		
		public function save($id, array $data){
			$this->samplingperiod = ($id > 0) ? ORM::factory("dao_samplingperiod",$id) : ORM::factory("dao_samplingperiod");
			$this->samplingperiod->pc_description = $data['pc_description'];
			$this->samplingperiod->k_value = $data['k_value'];
			$this->samplingperiod->save();
			return $this->samplingperiod->id;
	    }
		
		public function delete($id){
			$samplingperiod = ORM::factory("dao_samplingperiod",$id);
			$samplingperiod->delete();
			return $id;
		}
		
		
		/**
		*
		* return an entity sampling period by name
		* @param string $name
		* @return samplingperiod
		*/
		public function getByName($name){
			return ORM::factory("dao_samplingperiod")->where("pc_description","=",$name)->find();
		}
		
	}

?>